package com.powerunion.datacollection.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import net.sf.json.util.JSONUtils;

import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.TargetFieldsDao;
import com.powerunion.datacollection.model.TargetFields;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlParameter;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlParameterSet;
import com.powerunion.datacollection.persistence.db.jdbc.operator.SqlType;
import com.powerunion.datacollection.persistence.db.jdbc.operator.UpdateOperator;
import com.powerunion.util.log.LogHelper;
import com.powerunion.util.query.jdbc.QueryParameters;

public class TargetFieldsDaoImpl extends JdbcBase implements TargetFieldsDao{
	// 初始化日志类
	private static final Logger logger = LogHelper.getInstance().getLogger(
			TargetFieldsDaoImpl.class);
	
	public boolean delete(String categoryId) {

		if (logger.isDebugEnabled()) {
			logger.debug(Constants.INTOMETHOD + "delete");
		}
		String sql = "delete from dc_targets_field where targets_category_id = ? ";
		UpdateOperator update = new UpdateOperator(); 
		update.setSql(sql);
		update.addParameter(new SqlParameter(SqlType.VARCHAR,categoryId));
		
		update.access();
		
		int effectCount = update.getEffectCounts();
		
		if(effectCount > 0) {
			if (logger.isDebugEnabled()) {
				logger.debug("delete:" + Constants.DELETE + "指标报送值字段信息"
						+ Constants.SUCCESS);
			}
			return true;
		}else{
			if (logger.isDebugEnabled()) {
				logger.debug("delete:" + Constants.DELETE + "指标报送值字段信息"
						+ Constants.FAIL);
			}
			return false;
		}
	}

	public List getTargetFields(String categoryId) {
		logger.debug(Constants.INTOMETHOD + "getTargetFields");
		StringBuffer sqlStr = new StringBuffer();
		sqlStr
				.append(
						"select id,targets_category_id,field_name,ordr_no from dc_targets_field where targets_category_id = ? order by ordr_no");
		QueryParameters params = new QueryParameters();
		params.addParameter(categoryId, Types.VARCHAR);
		
		String sql = sqlStr.toString();
		List list = null;
		
		try {
			list = this.list(sql, params, "getTargetFieldsInfo");
		} catch (Exception e) {
			logger.error("getTargetFields:" + Constants.SEARCH + "TargetFieldsInfo"
					+ Constants.FAIL,e);
			throw new SystemException("search db record error!");
		}
		logger.debug("getTargetFields" + Constants.SEARCH
				+ "TargetFieldsInfo" + Constants.SUCCESS);
		return list;
	}
	
	public final TargetFields getTargetFieldsInfo(ResultSet rs) throws SQLException {
		TargetFields targetFields = new TargetFields();
		targetFields.setFieldName(rs.getString("field_name"));
		targetFields.setId(rs.getString("id"));
		targetFields.setTargetsCategoryId(rs.getString("targets_category_id"));
		targetFields.setDisplayOrder(rs.getInt("ordr_no"));
		
		return targetFields;
	}

	public boolean save(String[] fieldNames,String categoryId) {
		StringBuffer sql = new StringBuffer();
		sql.append("insert into dc_targets_field(id,targets_category_id,field_name,ordr_no)")
		   .append(" values(?,?,?,?)");
		
		UpdateOperator update = new UpdateOperator(); 
		update.setSql(sql.toString());
		int i = 1;
		for(int ii = 0;ii<fieldNames.length;ii++) {
			SqlParameterSet spSet = new SqlParameterSet();
			String id = String.valueOf(this.getSeq("dc_targets_field"));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,id));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,categoryId));
			spSet.addParameter(new SqlParameter(SqlType.VARCHAR,fieldNames[ii]));
			spSet.addParameter(new SqlParameter(SqlType.INTEGER,new Integer(i++)));
			
			update.addBatchParameter(spSet);
		}
		
		update.access();
		int[] effectCount = update.getBatchEffectCounts();
		if(effectCount != null && effectCount.length>0) {
			if (logger.isDebugEnabled()) {
				logger.debug("save:" + Constants.SAVE + "指标报送值字段信息"
						+ Constants.SUCCESS);
			}
			return true;
		}else{
			if (logger.isDebugEnabled()) {
				logger.debug("save:" + Constants.SAVE + "指标报送值字段信息"
						+ Constants.FAIL);
			}
			return false;
		}
	}


}
